import { defineStore } from "pinia";
import { ref } from "vue";
import { userInfoApi } from "@/api/user.js";
import router from "../router";
//用户模块
export const useUserStore = defineStore(
  "big-user",
  () => {
    const token = ref("");
    const setToken = (newToken) => {
      token.value = newToken;
    };
    /**
     * 移除 token
     *
     * @returns 无返回值
     */
    const removeToken = () => {
      token.value = "";
    };
    const user = ref({});
    const alreadyAsyncRoutes = ref(false);
    const updataAsyncRoutes = () => {
      alreadyAsyncRoutes.value = true;
    };
    const getUser = async () => {
      // const res = await userInfoApi();
      // user.value = res.data;
      user.value.MenuList = [
        {
          component: "",
          icon: "Tickets",
          id: 27,
          name: "project",
          title: "项目管理",
          path: "project",
          children: [
            {
              children: [],
              component: () => import("@/views/project/index.vue"),
              icon: "Connection",
              id: 29,
              name: "project:index",
              path: "/project/index",
              title: "项目列表",
              meta: ["项目管理", "项目列表"],
            },
            {
              children: [],
              component: () => import("@/views/project/templete.vue"),
              icon: "Odometer",
              id: 30,
              name: "project:templete",
              path: "/project/templete",
              title: "查验模板",
              meta: ["项目管理", "查验模板"],
            },
          ],
        },
        {
          children: [],
          component: () => import("@/views/notice/index.vue"),
          icon: "Promotion",
          id: 1,
          name: "sys:manage",
          path: "/notice",
          title: "公告管理",
          meta: ["公告管理"],
        },
        {
          component: "",
          icon: "PieChart",
          id: 31,
          name: "community",
          path: "community",
          title: "小区管理",
          children: [
            {
              children: [],
              component: () => import("@/views/community/communityList.vue"),
              icon: "Message",
              id: 1000,
              name: "community:communityList",
              path: "/community/communityList",
              title: "小区列表",
              meta: ["小区管理", "小区列表"],
            },
            {
              children: [],
              icon: "Message",
              id: 46,
              name: "community:ownerList",
              path: "/community/ownerList",
              component: () => import("@/views/community/ownerList.vue"),
              title: "业主列表",
              meta: ["小区管理", "业主列表"],
            },
          ],
        },
        {
          component: null,
          icon: "Crop",
          id: 33,
          name: "survey",
          path: "survey",
          title: "调查管理",
          children: [
            {
              id: 34,
              name: "survey:index",
              title: "调查列表",
              icon: "Location",
              path: "/survey/index",
              component: () => import("@/views/survey/index.vue"),
              children: [],
              meta: ["调查管理", "调查列表"],
            },
          ],
        },

        {
          id: 22,
          name: "vote:index",
          title: "投票管理",
          icon: "Promotion",
          path: "vote",
          component: "",
          children: [
            {
              id: 24,
              name: "vote:index",
              title: "投票列表",
              icon: "Promotion",
              path: "/vote/index",
              component: () => import("@/views/vote/index.vue"),
              children: [],
              meta: ["投票管理", "投票列表"],
            },
          ],
        },
        {
          component: "",
          icon: "Tickets",
          id: 27,
          name: "system:index",
          title: "系统管理",
          path: "mswssage",
          children: [
            {
              children: [],
              component: () => import("@/views/system/index.vue"),
              icon: "Connection",
              id: 29,
              name: "system:index",
              path: "/system/index",
              title: "用户管理",
              meta: ["系统管理", "用户管理"],
            },
          ],
        },
      ];
      addAsyncRoute();
    };
    const setUser = (obj) => {
      user.value = obj;
    };

    // 侧边栏是否展开
    const isExpand = ref(true);
    // 展开
    const toggleExpand = () => {
      isExpand.value = !isExpand.value;
    };
    const isScreen = ref(false);
    const toggleScreen = () => {
      isScreen.value = !isScreen.value;
    };
    const closeScreen = () => {
      isScreen.value = false;
    };

    const addAsyncRoute = () => {
      // 获取 前端定义的所有需要权限的路由
      const allRoutes = [];
      const files = import.meta.glob("@/router/modules/**/*.js", {
        eager: true,
      });
      console.log(files, "files");
      for (const key in files) {
        if (Object.hasOwnProperty.call(files, key)) {
          if (files[key].default) {
            allRoutes.push(...files[key].default);
          }
        }
      }
      const backRoutes = user.value.MenuList;
      allRoutes.forEach((x) => {
        router.addRoute("layout", x);
      });
      // console.log(backRoutes, "后端返回的");

      updataAsyncRoutes();
      console.log(allRoutes, "导入前端定义的所有动态路由");
      routerHandler(backRoutes, allRoutes);
    };
    const routerHandler = (userRoutes, allRoutes) => {
      // const realRoutrs = [];
      // allRoutes.forEach((v, i) => {
      //   userRoutes.forEach((item, index) => {
      //     if (item.title === v.title) {
      //       if (item.children && item.children.length > 0) {
      //         v.children = routerHandler(item.children, v.children);
      //       }
      //       realRoutrs.push(v);
      //     }
      //   });
      // });
      console.log(user.value.MenuList, "user");
      // user.value.MenuList = realRoutrs;
      // console.log(realRoutrs, "最终的路由");
      return user;
    };

    return {
      token,
      setToken,
      removeToken,
      user,
      getUser,
      setUser,
      isExpand,
      toggleExpand,
      isScreen,
      toggleScreen,
      closeScreen,
      addAsyncRoute,
      alreadyAsyncRoutes,
      updataAsyncRoutes,
    };
  },
  {
    persist: {
      storage: sessionStorage,
      paths: ["token", "user"],
    },
  }
);
